package com.example.cursor_orm.dao.impl;

import com.example.cursor_orm.dao.UserLogDAO;
import com.example.cursor_orm.model.UserLog;
import com.example.cursor_orm.util.DatabaseUtil;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Repository
public class UserLogDAOImpl implements UserLogDAO {
    @Override
    public int insert(UserLog log) {
        String sql = "INSERT INTO user_log (user_id, action, ip_address, log_time) VALUES (?, ?, ?, ?)";
        return DatabaseUtil.executeUpdate(sql, log.getUserId(), log.getAction(), log.getIpAddress(), log.getLogTime());
    }
    @Override
    public int deleteById(Integer id) {
        String sql = "DELETE FROM user_log WHERE id = ?";
        return DatabaseUtil.executeUpdate(sql, id);
    }
    @Override
    public int update(UserLog log) {
        String sql = "UPDATE user_log SET user_id = ?, action = ?, ip_address = ?, log_time = ? WHERE id = ?";
        return DatabaseUtil.executeUpdate(sql, log.getUserId(), log.getAction(), log.getIpAddress(), log.getLogTime(), log.getId());
    }
    @Override
    public UserLog findById(Integer id) {
        String sql = "SELECT * FROM user_log WHERE id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, id);
        return mapResultSetToUserLog(rs);
    }
    @Override
    public List<UserLog> findAll() {
        String sql = "SELECT * FROM user_log ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToList(rs);
    }
    @Override
    public List<UserLog> findByCondition(String condition) {
        String sql = "SELECT * FROM user_log WHERE " + condition + " ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToList(rs);
    }
    @Override
    public int count() {
        String sql = "SELECT COUNT(*) FROM user_log";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        try { if (rs != null && rs.next()) return rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); }
        return 0;
    }
    @Override
    public List<UserLog> findByUserId(Integer userId) {
        String sql = "SELECT * FROM user_log WHERE user_id = ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, userId);
        return mapResultSetToList(rs);
    }
    @Override
    public List<UserLog> findByAction(String action) {
        String sql = "SELECT * FROM user_log WHERE action = ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, action);
        return mapResultSetToList(rs);
    }
    @Override
    public List<UserLog> findByIpAddress(String ipAddress) {
        String sql = "SELECT * FROM user_log WHERE ip_address = ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, ipAddress);
        return mapResultSetToList(rs);
    }
    @Override
    public List<UserLog> findByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        String sql = "SELECT * FROM user_log WHERE log_time BETWEEN ? AND ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, startTime, endTime);
        return mapResultSetToList(rs);
    }
    @Override
    public List<UserLog> findByUserIdAndTimeRange(Integer userId, LocalDateTime startTime, LocalDateTime endTime) {
        String sql = "SELECT * FROM user_log WHERE user_id = ? AND log_time BETWEEN ? AND ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, userId, startTime, endTime);
        return mapResultSetToList(rs);
    }
    @Override
    public List<UserLog> findByUserIdAndAction(Integer userId, String action) {
        String sql = "SELECT * FROM user_log WHERE user_id = ? AND action = ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, userId, action);
        return mapResultSetToList(rs);
    }
    @Override
    public int deleteLogsBefore(LocalDateTime beforeTime) {
        String sql = "DELETE FROM user_log WHERE log_time < ?";
        return DatabaseUtil.executeUpdate(sql, beforeTime);
    }
    @Override
    public int countByUserId(Integer userId) {
        String sql = "SELECT COUNT(*) FROM user_log WHERE user_id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, userId);
        try { if (rs != null && rs.next()) return rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); }
        return 0;
    }
    private UserLog mapResultSetToUserLog(ResultSet rs) {
        try { if (rs != null && rs.next()) {
            UserLog log = new UserLog();
            log.setId(rs.getInt("id"));
            log.setUserId(rs.getInt("user_id"));
            log.setAction(rs.getString("action"));
            log.setIpAddress(rs.getString("ip_address"));
            Timestamp ts = rs.getTimestamp("log_time");
            if (ts != null) log.setLogTime(ts.toLocalDateTime());
            return log;
        } } catch (SQLException e) { e.printStackTrace(); }
        return null;
    }
    private List<UserLog> mapResultSetToList(ResultSet rs) {
        List<UserLog> list = new ArrayList<>();
        try { if (rs != null) while (rs.next()) {
            UserLog log = new UserLog();
            log.setId(rs.getInt("id"));
            log.setUserId(rs.getInt("user_id"));
            log.setAction(rs.getString("action"));
            log.setIpAddress(rs.getString("ip_address"));
            Timestamp ts = rs.getTimestamp("log_time");
            if (ts != null) log.setLogTime(ts.toLocalDateTime());
            list.add(log);
        } } catch (SQLException e) { e.printStackTrace(); }
        return list;
    }
} 